package com.chinact.saas.tenant.orggroup.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.chinact.platform.util.StringUtils;
import com.chinact.platform.util.database.IDUtil;
import com.chinact.platform.util.exception.SystemErrorExcetion;
import com.chinact.saas.tenant.orggroup.dao.IOrgGroupDao;
import com.chinact.saas.tenant.orggroup.domain.OrgGroupObj;
import com.chinact.saas.tenant.orggroup.service.IOrgGroupService;

/**
 * Title: ChinaCT SaaS
 * <p>
 * Description: 组织归口管理Service
 * <p>
 * Copyright: 2008
 * <p>
 * Company: Liaoning Open Data Communication Co.,Ltd.
 *
 * @author lihui
 * @version 1.0.0
 */
@Service("upOrgGroupService")
@Transactional(transactionManager = "ctSaaSIDSTransactionManager", propagation = Propagation.REQUIRED, rollbackFor = {
		Exception.class, RuntimeException.class })
public class OrgGroupServiceImpl implements IOrgGroupService {
	@Autowired(required = false)
	private IOrgGroupDao upOrgGroupDao;

	/**
	 * 保存对象
	 *
	 * @author lihui
	 * @date 2013-10-12 上午10:50:28
	 * @param obj
	 *            对象信息
	 * @throws SystemErrorExcetion
	 *             系统系统
	 */
	@Override
	public void saveOrUpdate(final OrgGroupObj obj) throws SystemErrorExcetion {
		if (obj == null) {
			return;
		}
		final String name = StringUtils.trimToEmpty(obj.getOrgGroupName());
		if (StringUtils.isEmpty(name)) {
			throw new SystemErrorExcetion("组织归口名称不能为空!");
		}

		if (StringUtils.isEmpty(obj.getId())) {
			obj.setId(IDUtil.getInstanse().getUUID());
		}
		if (StringUtils.isEmpty(obj.getLogicId())) {
			obj.setLogicId(IDUtil.getInstanse().getUUID());
		}
		obj.setCreateTime(new Date());
		// 看这里，业务没走完，不应该插入数据库【ok】
		this.upOrgGroupDao.save(obj);
		System.out.println("=================");
		// 看这里，有异常后，事务应该回滚【ok】
		int i = 1 / 0;
		// 看这里，自定义异常，同样回滚【x】
		//		throw new SystemErrorExcetion("组织归口名称不能为空!");
	}

}
